$(function(){
	var url={
		list:'{:url("adminapi/Driver/driverList")}',
		add:'{:url("adminapi/Driver/add")}',
		edit:'{:url("adminapi/Driver/edit")}'
	}
	var data={};
	{if $Think.get.driver_id}
	var data2={}
	data2.where_json=JSON.stringify({driver_id:'{$Think.get.driver_id}'})
	Load()
	$.ajax({
		type:'post',
		url:url.list,
		data:data2,
		success:function(res){
			Load(1)
			console.log(res)
			if(res.code==1){
				if(res.data.length==1){
					for(var key in res.data[0]){
						if($(`input[name="${key}"]`).attr('type')=='file'){
							data[key]=res.data[0][key]
							console.log(res.data[0][key])
							if(res.data[0][key]){
								$(`input[name="${key}"]`).closest('.file-w').find('.fileimg').attr('src',res.data[0][key])
							}else{
								
							}
							
						}else{
							$(`input[name="${key}"]`).val(res.data[0][key]);
						}
						if(key=='language'){
							console.log(res.data[0][key])
							var arr=res.data[0][key].split(',')
							console.log(arr)
							$('select[name="language[]"]').find('option').each(function(){
//								if($(this).val()==)
							
								for(var ii=0;ii<arr.length;ii++){
									if(arr[ii]==$(this).html()){
										$(this).attr('selected', true);
									}
								}
								
							})
							$('select[name="language[]"]').selected();
						}else{
							$(`select[name="${key}"]`).val(res.data[0][key]);
						}
						
//						$(`input[name="${key}"]`).val(res.data[0][key]);
					}
				}else{
					Err('数据有误')
				}
			}else{
				
			}
		},
		error:function(){
			Err('请求失败')
		}
	  })
	{else/}
	
	{/if}

	$('button').on('click',function(){
			if($(this).data('type')=='file'){
		 		$(this).parent().find('input[type=file]').click()
		 	}
	})
	
	$('input[type="file"]').on('change',function(){
		Load()
		var that=this;
		var file = $(that)[0].files[0]; 
			var formData = new FormData();
			var name = $(that).val();
			formData.append("image",file);
			formData.append("name",name);
			$.ajax({
				type:"post",
				url:"{:url('adminapi/Upload/uploadImg')}",
				data:formData,
				processData : false, 
				contentType : false,
				success:function(res){
					Load(1)
					console.log(res.image.pic_path)
					console.log($(that).attr('name'))
					data[$(that).attr('name')]=res.image.pic_path
					console.log(data)
				}
			});
	})
	$('.keep').on('click',function(){
		var check=1;
		Load()
		data.company_id=$('.company_all_id').val()
		$('select').each(function(){
			if($(this).attr('name')=="language[]"){
				console.log('language')
				var language="";
				for(var i=0;i<$(this).val().length;i++){
					if(language){
						language+=(','+$(this).val()[i])
						
					}else{
						language+=$(this).val()[i]
					}
				}
				data.language=language
				console.log(language)
			}else{
				data[$(this).attr('name')]=$(this).val();
			}
		})
		$('input').each(function(){
			var that=this
			if($(this).attr('type')=='file'){
				
			}else{
			if($(this).data('check')){
				//判断条件
				if($(this).data('check')=='empty'){
					if($(this).val()){
						data[$(this).attr('name')]=$(this).val();
					}else{
						check=0;
						Err($(this).msg);
					}
				}
			}else{
				if($(this).val()){
					data[$(this).attr('name')]=$(this).val();
				}
			}
			}
			
		})
		if(check){
			{if $Think.get.driver_id}
			data.driver_id='{$Think.get.driver_id}'
			{/if}
			console.log(data)
			$.ajax({
				type:'post',
				{if $Think.get.driver_id}
				url:url.edit,
				{else/}
				url:url.add,
				{/if}
				data:data,
				success:function(res){
					console.log(res)
					Load(1)
					if(res.code==1){
						
						{if $Think.get.driver_id}
						Msg('保存成功')
						window.history.go(0)
						{else/}
						Msg('添加成功')
						window.location.href="{:url('index')}"
						{/if}
					}else{
						Err(res.msg)
					}
				}
			})
		}
	});
	$('.car_rental').on('click', function() {
		choosecar("{:url('rent/index',['op'=>'search'])}");
	});
	$('.back').on('click', function() {
		window.history.back();
	});
})
